class Solution {
public:
    bool dfs(vector<int>& postorder, int l, int r) {
        if (l >= r)return 1;
        int x = l;
        int m = 0;
        while (postorder[x] < postorder[r])x++;
        m = x;
        while (postorder[x] > postorder[r])x++;
        return x == r && dfs(postorder, l, m - 1) && dfs(postorder, m, r - 1);
    }
    bool verifyTreeOrder(vector<int>& postorder) {
        int n = postorder.size();
        return dfs(postorder, 0, n - 1);
    }
};